from django.db import models
from django.contrib.auth.models import AbstractUser
from mptt.models import MPTTModel, TreeForeignKey
import os
import uuid
# Create your models here.

gender_choices = [
    (0, 'female'),
    (1, 'male'),
    (-1, 'unknown')
]


class User(AbstractUser):
    phone = models.CharField(max_length=32, unique=True, null=True, blank=True)
    email_validated = models.BooleanField(default=False)


def avatar_storage(instance, filename):
    filename = str(uuid.uuid1())
    return os.path.join('avatar', str(instance.id), filename)


class UserInfo(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='info')
    nickname = models.CharField(max_length=32, unique=True)
    gender = models.IntegerField(choices=gender_choices, default=-1)
    avatar = models.ImageField(upload_to=avatar_storage, default=None, null=True, blank=True)
    followers = models.ManyToManyField(
        'self', related_name='followees', through='UserRelation', symmetrical=False)


class UserRelation(models.Model):
    followee = models.ForeignKey(
        UserInfo, related_name='follower', on_delete=models.CASCADE)
    follower = models.ForeignKey(
        UserInfo, related_name='followee', on_delete=models.CASCADE)


class Article(models.Model):
    author = models.ForeignKey(
        UserInfo, related_name='articles', on_delete=models.CASCADE)
    content = models.TextField(max_length=200)
    created_at = models.DateTimeField(auto_now_add=True)
    last_modified_at = models.DateTimeField(auto_now=True)
    liked_by = models.ManyToManyField(
        UserInfo, related_name='my_like', through='UserLikeArticle')
    thumbuped_by = models.TextField(blank=True, null=True)

    class Meta:
        ordering = ['-last_modified_at']


class UserLikeArticle(models.Model):
    user = models.ForeignKey(UserInfo, on_delete=models.CASCADE)
    article = models.ForeignKey(Article, on_delete=models.CASCADE)

    class Meta:
        unique_together = [['user', 'article']]


class Comment(MPTTModel):
    author = models.ForeignKey(
        UserInfo, related_name='comments', on_delete=models.SET_NULL, null=True)
    article = models.ForeignKey(
        Article, related_name='comments', on_delete=models.CASCADE)
    content = models.TextField(max_length=200)
    created_at = models.DateTimeField(auto_now_add=True)
    parent = TreeForeignKey('self', on_delete=models.CASCADE,
                            null=True, blank=True, related_name='replies')
    is_deleted = models.BooleanField(default=False)
    thumbuped_by = models.TextField(blank=True, null=True)

    # class Meta:
    #     ordering = ['-created_at']
    class MPTTMeta:
        order_insertion_by = ['created_at']


class Notification(models.Model):
    # create notifications when:
    # someone follows you(delete)
    # someone you followed issues an article
    # someone(not you) makes a comment on your article
    # someone(not you) replies your comment
    owner = models.ForeignKey(
        UserInfo, related_name='received_notifications', on_delete=models.CASCADE)

    obj = models.ForeignKey(
        UserInfo, related_name='created_notifications', on_delete=models.SET_NULL, null=True)
    verb = models.CharField(max_length=15)

    new_article = models.ForeignKey(
        Article, related_name='article_create_notifications', on_delete=models.SET_NULL, null=True, blank=True)

    new_comment = models.ForeignKey(
        Comment, related_name='comment_create_notifications', on_delete=models.SET_NULL, null=True, blank=True)
    article = models.ForeignKey(Article, related_name='article_comment_notifications',
                                on_delete=models.SET_NULL, null=True, blank=True)

    reply = models.ForeignKey(Comment, related_name='reply_create_notifications',
                              on_delete=models.SET_NULL, null=True, blank=True) #可能跟comment字段重复了，暂时没用
    comment = models.ForeignKey(Comment, related_name='comment_reply_notifications',
                                on_delete=models.SET_NULL, null=True, blank=True)

    extra = models.TextField(blank=True, null=True, max_length=100)

    created_at = models.DateTimeField(auto_now_add=True)
    is_read = models.BooleanField(default=False)

    class Meta:
        ordering = ['is_read', '-created_at']
